home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / menu-new.tar / menu-new / menu / utils.c < prev    next >
C/C++ Source or Header  |  1993-04-08  |  4KB  |  297 lines

  1. # include "utils.h"
  2. # include <ctype.h>
  3. /*
  4.     This file contains basic low level utilities.
  5. */
  6.  
  7. int get_term()
  8. /*
  9.        This routine gets the terminal specific escape sequences
  10.        and saves them in the appropriate variable for use by
  11.        tputs() and tgoto()
  12. */
  13. {
  14.     char bp[1024], termtype[MAXL];
  15.     static char buf[100];
  16.     char *buf_ptr = buf;
  17.     int status;
  18.  
  19.  
  20.     initscr();
  21.     if(getenv("TERM") != '\0')
  22.        (void) strcpy(termtype, getenv("TERM"));
  23.  
  24. # ifdef TERM_INFO
  25.     setupterm(termtype,1,&status);
  26.     if(status != 1)
  27.        return(-1);
  28.  
  29.     KU = key_up;
  30.     KD = key_down;
  31.     KL = key_left;
  32.     KR = key_right;
  33.     
  34. # else TERM_INFO
  35.  
  36.     if(tgetent(bp,termtype) != 1)
  37.        return(-1);
  38.     if((KU = tgetstr("ku",&buf_ptr)) == (char *)'\0')
  39.        return (1);
  40.     if((KD = tgetstr("kd",&buf_ptr)) == (char *)'\0')
  41.        return (1);
  42.     if((KL = tgetstr("kl",&buf_ptr)) == (char *)'\0')
  43.        return (1);
  44.     if((KR = tgetstr("kr",&buf_ptr)) == (char *)'\0')
  45.        return (1);
  46. # endif TERM_INFO
  47.     return(0);
  48. }
  49.  
  50. outc(c)
  51. char c;
  52. /*
  53.     This routine puts a single character out to the screen
  54.     at the current location.
  55. */
  56. {
  57.     addch(c);
  58.     refresh();
  59. }
  60.  
  61. move_csr(x,y)
  62. int x,y;
  63. /*
  64.        This routine is used to move the cursor to a new position
  65.        on the screen.
  66. */
  67. {
  68.     move(y,x);
  69.     refresh();
  70. }
  71.  
  72. clr_scr()
  73. /*
  74.     This routine clears the screen and leaves the cursor in the
  75.     upper left corner of the screen.
  76. */
  77. {
  78.     clear();
  79.     refresh();
  80. }
  81.  
  82. start_rev()
  83. /*
  84.     Start the reverse video mode.
  85. */
  86. {
  87.     standout();
  88. }
  89.  
  90. end_rev()
  91. /*
  92.     End the reverse video mode.
  93. */
  94. {
  95.     standend();
  96. }
  97.  
  98. print_str(string)
  99. char *string;
  100. /*
  101.     Print an entire string to the screen.
  102. */
  103. {
  104.     printw("%s",string);
  105.     refresh();
  106. }
  107.  
  108. hi_lite(string)
  109. char *string;
  110. /*
  111.     Hi-lite a string that will be printed.
  112. */
  113. {
  114.     start_rev();
  115.     print_str(string);
  116.     end_rev();
  117. }
  118.  
  119. save_tty() 
  120. /*
  121.     Save Terminal characteristics Reset by call to reset_tty().
  122. */
  123. {
  124.     savetty();
  125. }
  126.  
  127. reset_tty() 
  128. /*
  129.     This routine returns the terminal to the same state it
  130.     had before this program was invoked.
  131. */
  132. {
  133.     resetty();
  134. }
  135.  
  136. my_crmode()
  137. /*
  138.     Turn on CRmode.
  139. */
  140. {
  141.     crmode();
  142. }
  143.  
  144. no_crmode()
  145. /*
  146.     Turn off CRmode.
  147. */
  148. {
  149.     nocrmode();
  150. }
  151.  
  152. my_echo()
  153. /*
  154.     Turn echo back on.
  155. */
  156. {
  157.     echo();
  158. }
  159.  
  160. no_echo()
  161. /*
  162.     Turn off echo.
  163. */
  164. {
  165.     noecho();
  166. }
  167.  
  168. no_cbreak()
  169. /*
  170.     Take the terminal out of cbreak mode.
  171. */
  172. {
  173. /*
  174.     noraw();
  175. */
  176. }
  177.  
  178. my_cbreak()
  179. /*
  180.     Place the Terminal in Cbreak mode.
  181. */
  182. {
  183. /*
  184.     raw();
  185. */
  186. }
  187.  
  188. void quit()
  189. {
  190.     echo();
  191.     clr_scr();            /* Clear screen routine             */
  192.     move_csr(0,LAST_LINE);
  193.     my_quit();
  194.     nocrmode();
  195.     echo();
  196.     exit(0);
  197. }
  198.     
  199. rm_lf(array)
  200. char *array;
  201. /*    
  202.     Remove Trailing line feed from a line.
  203. */
  204. {
  205.     if(array[strlen(array)-1] == '\n')
  206.       array[strlen(array)-1] = '\0';
  207. }
  208.  
  209. read_str(string,max_length)
  210. char *string;
  211. int max_length;
  212. {
  213.     char *str;
  214.     int length = 0;
  215.  
  216.     str = string;
  217.     while((*str =my_getchar()) != LF && *str != RETURN && length <= max_length){
  218.        if(*str ==  DEL  || *str == BS)
  219.           if(*str != *string){
  220.          outc(DEL);
  221.          outc(SPACE);
  222.          outc(DEL);
  223.          *str--;
  224.          length--;
  225.           }else
  226.          continue;
  227.        else{
  228.           if(length == max_length)
  229.             outc(0x07);
  230.           else{
  231.             outc(*str);
  232.             *str++;
  233.             length++;
  234.           }
  235.        }
  236.     }
  237.     *str = '\0';
  238. }
  239.  
  240. clear_array(array)
  241. char *array;
  242. {
  243.     for(;*array != '\0';*array++)
  244.         *array = '\0';
  245. }
  246.  
  247. int get_input()
  248. {
  249.     char c, buffer[80];
  250.     int ct = 0, len;
  251.  
  252.     if((len = strlen(KU)-1) < 2)
  253.       len = 1;
  254.     while((c = my_getchar()) && (ct < len)){
  255.        if((c == 0x1b) || (ct > 0)){
  256.          buffer[ct] = c;
  257.          ct++; 
  258.        }else
  259.          return(c);
  260.     }
  261.     buffer[ct] = c;
  262.     buffer[ct+1] = '\0';
  263.     if((strcmp(KU,buffer)) == 0)
  264.       return(UP);
  265.     else
  266.       if((strcmp(KD,buffer)) == 0)
  267.         return(DOWN);
  268.       else
  269.         if((strcmp(KL,buffer)) == 0)
  270.           return(LEFT);
  271.         else
  272.           if((strcmp(KR,buffer)) == 0)
  273.             return(RIGHT);
  274.           else
  275.             return('\0');
  276. }
  277.  
  278. int my_getchar()
  279. {
  280.     return(getch());
  281. }
  282. /*****************************************************************************
  283.  Prepare for Quit Menu, and exit (restore original settings).
  284.  *****************************************************************************/
  285.  
  286. my_quit()
  287. {
  288.     clr_scr();    /* Clear screen routine             */
  289.     move_csr(0,LAST_LINE);
  290. /*    my_quit();*/
  291. /*    my_echo();*/
  292.     reset_tty();
  293.     endwin();
  294. /*    exit(0);*/
  295. }
  296.  
  297.